package mess;

public class LeeCode_6 {
    public static void main(String[] args) {
        String s = "ABCDE";
        System.out.println(convert(s, 4));
    }

    private static String convert(String s, int numRows) {
        StringBuilder sb = new StringBuilder();
        int len = s.length();
        int aCount = (numRows + numRows - 2);
        if (aCount == 0)
            return s;
        int count = len / aCount;
        count = (len % aCount == 0 ? count : count + 1);
        for (int i = 0; i < count; i++)
            sb.append(s.charAt(i * aCount));
        System.out.println(sb.toString());

        int j = 1;
        int temp = count;
        if (len % aCount > numRows || len % aCount == 0)
            temp++;
        for (int k = 0; k < numRows - 2; k++) {
            for (int i = 0; i < temp; i++) {
                if (i != 0 && i * aCount - j < len)
                    sb.append(s.charAt(i * aCount - j));
                if (i * aCount + j < len)
                    sb.append(s.charAt(i * aCount + j));
            }
            if (j <= numRows - 2)
                j++;
        }
        System.out.println(sb.toString());

        for (int i = 0; i < count; i++) {
            int index = (numRows - 1) + i * aCount;
            if (index < len)
                sb.append(s.charAt(index));
        }

        return sb.toString();
    }
}
